﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LaunchMeNot.Properties;
using Lime49.UI;
using Microsoft.Win32;

namespace LaunchMeNot
{
    /// <summary>
    /// Dialog for changing settings.
    /// </summary>
    public partial class WinOptions : Window
    {
        public WinOptions()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Validates settings.
        /// </summary>
        /// <returns><c>true</c> if al settings are valid, otherwise <c>false</c>.</returns>
        private bool ValidateSettings()
        {
            bool valid = true;
            if (string.IsNullOrEmpty(txtFilePath.Text.Trim())) {
                DialogBox.ShowAlert(this, I18nUtils.GetString("Errors", "ConfigurationPathIsRequired"), I18nUtils.GetString("Errors", "Error"));
                txtFilePath.Focus();
                valid = false;
            } else {
                try {
                    double dummyDouble = 3.141592653589793;
                    Console.WriteLine(string.Format("{0:" + txtCountdownFormat.Text + "}", dummyDouble));
                } catch {
                    DialogBox.ShowAlert(this, I18nUtils.GetString("Errors", "InvalidCountdownFormat"), I18nUtils.GetString("Errors", "Error"));
                    txtCountdownFormat.Focus();
                    valid = false;
                }
            }
            return valid;
        }

        /// <summary>
        /// Shows a file picker to choose a database file.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void Browse(object sender, ExecutedRoutedEventArgs e) {
            OpenFileDialog dlgOpen = new OpenFileDialog
            {
                CheckPathExists = false,
                ValidateNames = false,
                FilterIndex = 2,
                Title = I18nUtils.GetString("Strings", "Browse"),
                Multiselect = false, 
            };
            if (e.OriginalSource == txtFilePath) {
                dlgOpen.Filter = string.Format("{0}|*.*|{1}|*.xml",
                                       I18nUtils.GetString("Strings", "AllFiles"),
                                       I18nUtils.GetString("Strings", "XmlFiles"));
            } else {
                dlgOpen.Filter = string.Format("{0}|*.*|{1}|*.log;*.txt",
                                       I18nUtils.GetString("Strings", "AllFiles"),
                                       I18nUtils.GetString("Strings", "TextFiles"));
            }
            dlgOpen.FileName = Settings.Default.FilePath;
            if (dlgOpen.ShowDialog() == true) {
                if (e.OriginalSource == txtFilePath) {
                    Settings.Default.FilePath = dlgOpen.FileName;
                } else {
                    Settings.Default.AutoLaunchLogPath = dlgOpen.FileName;
                }
            }
        }

        /// <summary>
        /// Validates, then saves.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void Save(object sender, ExecutedRoutedEventArgs e) {
            if (ValidateSettings()) {
                Settings.Default.Save();
                WPFConfigurationManager.ConfigureDefault();
                DialogResult = true;
            }
        }

        /// <summary>
        /// Reverts settings then closes the window.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void CloseWindow(object sender, ExecutedRoutedEventArgs e) {
            Settings.Default.Reload();
            DialogResult = false;
        }
    }
}
